/**
 * Created by isoft-work 5 on 1/3/2015.
 */
$(document).ready(function(){
   _ruleOutput = null;
   getRuleOut();
    bindEventRule();
});

function bindEventRule() {
   ISS.EVENT.bindEvent('#rule_button_new', 'click', function () {
      var _ruleInput = {};
      if(validateRule(_ruleInput)) {
         getRule(_ruleInput);
      }
      else {
         alert('Chưa nhập đầy đủ thông tin!');
      }
   });
}

function getRuleOut() {
   ISS.CONTROL.lockScreen('#content');
   ISS.BACKEND.request({
      url: SERVER.contextPath + '/service/admin/consultant/getRuleOut/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseInfo = data.responseObject;
         if(responseInfo) {
            mapTapLuatKetHopToView(responseInfo);
         }
         else {
            alert('Không có luật nào');
         }

         ISS.CONTROL.unLockScreen('#content');
      }
   });
}

function validateRule(ruleInput) {
   var _minSupp = ISS.DOM.getVal('#rule_min_supp');
   var _minConfig = ISS.DOM.getVal('#rule_min_config');
   var result = false;
   if(!_minSupp) {
      result = false;
   }
   if(!_minConfig) {
      result = false;
   }
   if(_minSupp && _minConfig) {
      result = true;
      mapViewToRuleInput(ruleInput);
   }
   return result;
}

function getRule() {
   var _ruleInput = {};
   mapViewToRuleInput(_ruleInput);
   ISS.CONTROL.lockScreen('#content');
   ISS.BACKEND.request({
      url: SERVER.contextPath + '/service/admin/consultant/getRule/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(_ruleInput),
      success: function (data) {
         var responseInfo = data.responseObject;

         if(responseInfo) {
            _ruleOutput = responseInfo;
            mapRuleInputToView(responseInfo);
         }
         else {
            ISS.DOM.html('#message', "Không có tập luật nào!");
         }

         ISS.CONTROL.unLockScreen('#content');
      }
   });
}

function bindEventRuleUpdate() {
   ISS.EVENT.bindEvent('#rule_update', 'click', function () {
      //ISS.CONTROL.lockScreen('#content');
      ISS.BACKEND.request({
         url: SERVER.contextPath + '/service/admin/consultant/updateRule/',
         headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
         },
         type: 'POST',
         dataType: 'json',
         data: JSON.stringify(_ruleOutput),
         success: function (data) {
            //ISS.CONTROL.unLockScreen('#content');
            alert('Cập nhật thành công!');
         }
      });
   });
}

function mapViewToRuleInput(ruleInput) {
   ruleInput.minSupp = ISS.DOM.getVal('#rule_min_supp');
   ruleInput.minConfig = ISS.DOM.getVal('#rule_min_config');
}

function mapRuleInputToView(ruleOnput) {
   var html = '';
   for(var i = 0; i < ruleOnput.length; i++) {
      ruleOnput[i].stt = i + 1;
      html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RuleTable.listRule, ruleOnput[i]);
      delete ruleOnput[i].stt;
   }
   ISS.DOM.html('#list_rule', html);
   ISS.DOM.show('#paging');
   ISS.DOM.show('#rule_update_show');
   ISS.DOM.pagingAdmin('#result_rule');

   bindEventRuleUpdate();
}

function mapTapLuatKetHopToView(ruleOnput) {
   var html = '';
   for(var i = 0; i < ruleOnput.length; i++) {
      ruleOnput[i].stt = i + 1;
      html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RuleTable.listRuleTwo, ruleOnput[i]);
      delete ruleOnput[i].stt;
   }
   ISS.DOM.html('#list_rule', html);
   ISS.DOM.show('#paging');
   ISS.DOM.hide('#rule_update_show');
   ISS.DOM.pagingAdmin('#result_rule');
}

function drawChart(ruleOnput) {
   var _html = '\nBiểu đồ\tTập luật';
   for(var i = 0; i < ruleOnput.length; i++) {
      _html += '\n' + ruleOnput[i].luat;
      _html += '\t' + ruleOnput[i].phanTram + '%';
   }
   ISS.DOM.html('#tsv', _html);
   ISS.GRAPH.chart('tsv');
}
